package com.dy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dy.mapper.DyBlogMapper;
import com.dy.mapper.DyBlogTagMapper;
import com.dy.mapper.DyTagMapper;
import com.dy.pojo.entity.DyBlogEntity;
import com.dy.pojo.entity.DyBlogTagEntity;
import com.dy.pojo.entity.DyTagEntity;
import com.dy.pojo.vo.DyShowTagVO;
import com.dy.service.DyTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName:
 * @Author: 仄言in
 * @Desctiption: (描述)
 * @Date: 2021/6/9 15:54
 * @Version 1.0 (版本)
 */
@Service
public class DyTagServiceImpl extends ServiceImpl<DyTagMapper, DyTagEntity> implements DyTagService {
    @Autowired
    private DyTagMapper dyTagMapper;
    @Autowired
    private DyBlogTagMapper dyBlogTagMapper;
    @Autowired
    private DyBlogMapper dyBlogMapper;

    @Override
    public Page<DyTagEntity> queryTag(Integer page, Integer pageSize) {
        Page<DyTagEntity> page1 = new Page<>(page, pageSize);
        Page<DyTagEntity> selectPage = dyTagMapper.selectPage(page1, null);
        return selectPage;
    }

    @Override
    public boolean createTag(String name) {
        String code="G"+System.currentTimeMillis();
        DyTagEntity dyTagEntity = new DyTagEntity();
        dyTagEntity.setName(name)
                .setCode(code);
        dyTagMapper.insert(dyTagEntity);
        return true;
    }

    @Override
    public boolean deleteTag(Long id) {
        dyTagMapper.deleteById(id);
        return true;
    }

    @Override
    public DyTagEntity selectTagById(Long id) {
        DyTagEntity tagEntity = dyTagMapper.selectOne(new QueryWrapper<DyTagEntity>().eq("id", id));
        return tagEntity;
    }

    @Override
    public boolean updateTag(Long id, String name) {
        DyTagEntity dyTagEntity = dyTagMapper.selectOne(new QueryWrapper<DyTagEntity>().eq("id", id));
        dyTagEntity.setName(name).setId(id);
        dyTagMapper.updateById(dyTagEntity);
        return true;
    }

    @Override
    public List<DyShowTagVO> listShowTag() {
        List<DyTagEntity> selectList = dyTagMapper.selectTags();
        List<DyShowTagVO> dyShowTagList = new ArrayList<DyShowTagVO>();
        for (int i = 0; i < selectList.size(); i++) {
            List<DyBlogTagEntity> tagcodelist = dyBlogTagMapper.selectList(new QueryWrapper<DyBlogTagEntity>().eq("tag_code", selectList.get(i).getCode()));
            DyShowTagVO dyShowTagVO = new DyShowTagVO();
            dyShowTagVO.setName(selectList.get(i).getName()).setDyblogs(tagcodelist);
            dyShowTagList.add(dyShowTagVO);
        }
        return dyShowTagList;
    }

    @Override
    public List<DyTagEntity> selectShowTags(Long id) {
        DyBlogEntity selectOne = dyBlogMapper.selectOne(new QueryWrapper<DyBlogEntity>().eq("id", id));
        List<DyBlogTagEntity> selectList = dyBlogTagMapper.selectList(new QueryWrapper<DyBlogTagEntity>().eq("blog_code", selectOne.getCode()));
        List<DyTagEntity> dytagList = new ArrayList<>();
        for (int i = 0; i < selectList.size(); i++) {
            DyTagEntity tagEntity = dyTagMapper.selectOne(new QueryWrapper<DyTagEntity>().eq("code", selectList.get(i).getTagCode()));
            dytagList.add(tagEntity);
        }
        return dytagList;
    }
}
